package com.data.modules.online.cgform.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;
import com.data.common.api.vo.Result;
import com.data.common.constant.CommonConstant;
import com.data.common.system.base.controller.BootController;
import com.data.common.system.query.QueryGenerator;
import com.data.modules.online.cgform.entity.OnlCgformIndex;
import com.data.modules.online.cgform.service.IOnlCgformIndexService;
import com.data.modules.online.cgform.util.CgformUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "onlCgformIndexController")
@RequestMapping("/online/cgform/index")
@RestController
public class OnlCgformIndexController {

    private static final Logger f103a = LoggerFactory.getLogger(OnlCgformIndexController.class);
    @Autowired
    private IOnlCgformIndexService onlCgformIndexService;

    /** online 表单开发 field列表
     * @param headId
     * @return
     */
    @ApiOperation("online 表单开发 field列表")
    @GetMapping({"/listByHeadId"})
    public Result<?> listByHeadId(@RequestParam("headId") String headId) {
        QueryWrapper<OnlCgformIndex> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cgform_head_id", headId);
        queryWrapper.eq("del_flag", CommonConstant.DEL_FLAG_0);
        queryWrapper.orderByDesc("create_time");
        return Result.ok(this.onlCgformIndexService.list(queryWrapper));
    }

    @GetMapping({"/list"})
    public Result<IPage<OnlCgformIndex>> mo378a(OnlCgformIndex onlCgformIndex, @RequestParam(defaultValue = "1", name = "pageNo") Integer num, @RequestParam(defaultValue = "10", name = "pageSize") Integer num2, HttpServletRequest httpServletRequest) {
        Result<IPage<OnlCgformIndex>> result = new Result<>();
        Wrapper initQueryWrapper = QueryGenerator.initQueryWrapper(onlCgformIndex, httpServletRequest.getParameterMap());
        IPage page = this.onlCgformIndexService.page(new Page((long) num.intValue(), (long) num2.intValue()), initQueryWrapper);
        result.setSuccess(true);
        result.setResult(page);
        return result;
    }

    @PostMapping({"/add"})
    public Result<OnlCgformIndex> mo377a(@RequestBody OnlCgformIndex onlCgformIndex) {
        Result<OnlCgformIndex> result = new Result<>();
        try {
            this.onlCgformIndexService.save(onlCgformIndex);
            result.success("添加成功！");
        } catch (Exception e) {
            f103a.error(e.getMessage(), e);
            result.error500("操作失败");
        }
        return result;
    }

    @PutMapping({"/edit"})
    public Result<OnlCgformIndex> mo380b(@RequestBody OnlCgformIndex onlCgformIndex) {
        Result<OnlCgformIndex> result = new Result<>();
        if (((OnlCgformIndex) this.onlCgformIndexService.getById(onlCgformIndex.getId())) == null) {
            result.error500("未找到对应实体");
        } else if (this.onlCgformIndexService.updateById(onlCgformIndex)) {
            result.success("修改成功!");
        }
        return result;
    }

    @DeleteMapping({"/delete"})
    public Result<OnlCgformIndex> mo379b(@RequestParam(name = "id", required = true) String str) {
        Result<OnlCgformIndex> result = new Result<>();
        if (((OnlCgformIndex) this.onlCgformIndexService.getById(str)) == null) {
            result.error500("未找到对应实体");
        } else if (this.onlCgformIndexService.removeById(str)) {
            result.success("删除成功!");
        }
        return result;
    }

    @DeleteMapping({"/deleteBatch"})
    public Result<OnlCgformIndex> mo381c(@RequestParam(name = "ids", required = true) String str) {
        Result<OnlCgformIndex> result = new Result<>();
        if (str == null || "".equals(str.trim())) {
            result.error500("参数不识别！");
        } else {
            this.onlCgformIndexService.removeByIds(Arrays.asList(str.split(CgformUtil.f231E)));
            result.success("删除成功!");
        }
        return result;
    }

    @GetMapping({"/queryById"})
    public Result<OnlCgformIndex> mo382d(@RequestParam(name = "id", required = true) String str) {
        Result<OnlCgformIndex> result = new Result<>();
        OnlCgformIndex onlCgformIndex = (OnlCgformIndex) this.onlCgformIndexService.getById(str);
        if (onlCgformIndex == null) {
            result.error500("未找到对应实体");
        } else {
            result.setResult(onlCgformIndex);
            result.setSuccess(true);
        }
        return result;
    }
}
